﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Communication.Base;

namespace UtilZ.Dotnet.Ex.Communication.Net.Web
{
    /// <summary>
    /// http服务器配置
    /// </summary>
    public class HttpServerConfig
    {
        /// <summary>
        /// Accept模式
        /// </summary>
        public ThreadMode AcceptMode { get; set; } = ThreadMode.Async;

        /// <summary>
        /// 服务器监听IPEndPoint
        /// </summary>
        public IPEndPoint ListenEndPoint { get; set; }

        /// <summary>
        /// SSL证书
        /// </summary>
        public X509Certificate Certificate { get; set; }

        /// <summary>
        /// 临时文件目录
        /// </summary>
        public string TempFileDirectory { get; set; }

        /// <summary>
        /// http连接保持时长，单位:毫秒
        /// </summary>
        public int HttpConnectionKeepMilliseconds { get; set; } = 30000;

        private int _maxConnections = 1000;
        /// <summary>
        /// 最大连接数,小于等于0无限制.默认1000个
        /// </summary>
        public int MaxConnections
        {
            get { return _maxConnections; }
            set { _maxConnections = value; }
        }

        /// <summary>
        /// 是未经过处理的连接请求队列可以容纳的最大数目
        /// </summary>
        public int Backlog { get; set; } = 10;


        /*
        private int _receiveDataSize = 4096;
        /// <summary>
        /// 单次接收数据包大小
        /// </summary>
        public int ReceiveDataSize
        {
            get { return _receiveDataSize; }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), "单次接收数据包不能小于等于0");
                }

                if (value > this._bufferSize)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"单次接收数据包大小不能大于解析缓存大小\"{this._bufferSize}\"");
                }

                _receiveDataSize = value;
            }
        }

        private int _bufferSize = 65536;
        /// <summary>
        /// 解析缓存大小，默认(65536=64k>tcp_max_mtu=65507)
        /// 注:
        /// 当缓存大小大于等于MTU_MAX2倍时,每次接收最大值为MTU_MAX
        /// 否则为缓存大小的1/4
        /// </summary>
        public int BufferSize
        {
            get { return _bufferSize; }
            set
            {
                //最小缓存4k
                if (value <= this._receiveDataSize)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"解析缓存不能小于单次接收数据包大小\"{this._receiveDataSize}\"");
                }

                _bufferSize = value;
            }
        }

        */



        /// <summary>
        /// 构造函数
        /// </summary>
        public HttpServerConfig()
        {

        }


        /// <summary>
        /// 获取临时文件目录
        /// </summary>
        /// <returns></returns>
        internal string GetTempFileDirectory()
        {
            string tempFileDirectory = this.TempFileDirectory;
            if (string.IsNullOrWhiteSpace(tempFileDirectory))
            {
                tempFileDirectory = PathEx.GetTypeAssemblyDirectory(this.GetType());
                tempFileDirectory = PathEx.Combine(PathType.Local, tempFileDirectory, "temp");
            }

            return tempFileDirectory;
        }
    }
}
